/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.RecomendadorForm;
import com.uca.sisbec.helpers.manto.RecomendadorHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Susy
 */
public class RecomendadorAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RecomendadorForm rf = (RecomendadorForm) form;
        rf.setEstado(true);
        rf.setEmails(new String[0]);
        rf.setPhones(new String[0]);
        request.setAttribute("save", "new");
        return mapping.findForward("success");
    }

    public ActionForward initBusqueda(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mapping.findForward("search");
    }

    public ActionForward AgregarRecomendador(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RecomendadorForm rf = (RecomendadorForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        RecomendadorHelper helper = new RecomendadorHelper(getSession(request));
        if (helper.validateRecomendador(rf)){
            int codAi = helper.insertarRecomendandorDB(rf);
            rf.setCodigo(codAi);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("recomendador.nuevo"));
            request.setAttribute("msg", msg);
            request.setAttribute("save", "update");
        }else{
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("recomendador.existe", rf.getCodigo()));
            request.setAttribute("errors", errors);
            request.setAttribute("save", "new");
        }
        return mapping.findForward("success");
    }

    public ActionForward BuscarRecomendador(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RecomendadorForm rf = (RecomendadorForm) form;
        RecomendadorHelper helper = new RecomendadorHelper(getSession(request));
        request.setAttribute("recomendadorData", helper.buscarRecomendadorDB(rf));
        return mapping.findForward("search");
    }

    public ActionForward SeleccionarRecomendador(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RecomendadorForm rf = (RecomendadorForm) form;
        RecomendadorHelper helper = new RecomendadorHelper(getSession(request));
        helper.buscarRecomendadorDB(rf.getCodigo(), rf);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward ModificarRecomendador(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RecomendadorForm rf = (RecomendadorForm) form;
        RecomendadorHelper helper = new RecomendadorHelper(getSession(request));
        helper.modificarRecomendadorDB(rf);
        ActionMessages msg = new ActionMessages();
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("recomendador.modificado"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
}
